/**
 * 
 */
package org.dbwebx.adapter.jcr.util;

import org.dbwebx.domain.DbUnit;
import org.dbwebx.persistence.JCRDBUnit;

/**
 * @author Izzet_Mustafayev
 * 
 */
public class JcrUtils {

	public static String generateName(final DbUnit dbUnit) {
		String name = "";
		name = dbUnit.getName();
		return name;
	}

	/**
	 * Creates relative path from given path.
	 * 
	 * @param nodePath
	 *            the path to transform
	 * @return transformed path
	 */
	public static String prepareRelativeNodePath(final String nodePath) {
		String newNodePath = "";
		if (!"".equals(nodePath) && null != nodePath
				&& nodePath.indexOf("/") == 0) {
			newNodePath = nodePath.substring(1, nodePath.length());
		}
		return newNodePath;
	}

	/**
	 * Creates base path for the {@link JCRDBUnit}.
	 * 
	 * @param dbUnit
	 *            the {@link JCRDBUnit} to create base path for
	 * @return created base path
	 */
	public static String createBasePath(final JCRDBUnit dbUnit) {
		String path = "/";
		if (null != dbUnit && null != dbUnit.getBundle()) {
			if (null != dbUnit.getBundle().getSourceNode()) {
				path += dbUnit.getBundle().getSourceNode().getIdentity() + "_"
						+ dbUnit.getBundle().getSourceNode().getName();
			}
			if (!"/".equals(path.substring(path.length() - 1))) {
				path += "/";
			}
			path += dbUnit.getBundle().getIdentity() + "_"
					+ dbUnit.getBundle().getName();
		}
		return path;
	}

	/**
	 * Creates base path for given {@link DbUnit}.
	 * 
	 * @param dbUnit
	 *            the {@link DbUnit} to create base path for
	 * @return created base path
	 */
	public static String createBasePath(final DbUnit dbUnit) {
		String path = "/";
		if (null != dbUnit && null != dbUnit.getBundle()) {
			if (null != dbUnit.getBundle().getSourceNode()) {
				path += dbUnit.getBundle().getSourceNode().getIdentity() + ":"
						+ dbUnit.getBundle().getSourceNode().getName();
			}
			path += dbUnit.getBundle().getIdentity() + ":"
					+ dbUnit.getBundle().getName();
		}

		return path;
	}
}
